<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('题库列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>所属科目：</label>
                            <input type="text" name="subjectId"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>


        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
        <div id="show-banks">

        </div>


    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:bank:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:bank:remove')}]];
    var prefix = ctx + "system/bank";
    var selectedBank = [[${selectedBank}]];
    var id = [[${id}]];
    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "题库",
            onCheck: onChangeBank,
            onUncheck: onChangeBank,
            onCheckAll: onChangeBank,
            onUncheckAll: onChangeBank,
            columns: [{
                checkbox: true,
                formatter:function (a,row,c) {
                    for (var i = 0; i < selectedBank.length; i++) {
                        if(row.id==selectedBank[i]){
                            return true;
                        }
                    }
                }
            },
                {
                    field: 'id',
                    title: '题库id',
                    visible: false
                },
                {
                    field: 'subjectName',
                    title: '所属科目'
                },
                {
                    field: 'title',
                    title: '标题描述'
                },
                {
                    field: 'score',
                    title: '分值'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                       return actions.join('');
                    }
                }]
        };
        $.table.init(options);
        showBanks();


    });
    function onChangeBank(data) {
        //查找表中所有的
        var rowsData =$("#bootstrap-table").bootstrapTable('getData');
        for (var i = 0; i < rowsData.length; i++) {

            if(rowsData[i][0]){//被选中
                var bool = false;
                for (var j = 0; j < selectedBank.length; j++) {
                    if(rowsData[i].id==selectedBank[j]){
                        bool = true;
                    }
                }
                if(!bool){
                    selectedBank.push(rowsData[i].id);
                }
            }else{//未被选中
                var bool = false;
                for (var j = 0; j < selectedBank.length; j++) {
                    if(rowsData[i].id==selectedBank[j]){
                        bool = true;
                    }
                }
                if(bool){
                    selectedBank = selectedBank.filter(function(item) {return item !== rowsData[i].id
                    });
                }
            }
            debugger;
            //console(selectedBank);

        }


        showBanks();
        changeData();

    }
    function showBanks() {
        $("#show-banks").html("");
        var html = "";
        for (var i = 0; i < selectedBank.length; i++) {
            html+="<span>"+selectedBank[i]+"</span>"
        }
        $("#show-banks").html(html);
    }
    function changeData() {
        $.ajax({ url:  "/system/paper" + "/changeBank",type:"post",data:{"selectedBank":selectedBank.join(","),"id":id}, success: function(){

            }});
    }
</script>
<style>
   /* #show-banks span{
        display: block;
        width: 100px;
        height: 100px;
        text-align: center;
        background-color: aqua;
        border: 2px solid black;
    }*/
</style>
</body>
</html>